<template>
    <el-card shadow="never" class="border-0">
        <!-- 新增刷新 -->
        <ListHeader @refresh="getData" @create="handleCreate"></ListHeader>


        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%" v-loading="loading">
            <el-table-column prop="name" label="名称" />

            <el-table-column prop="integral" label="積分" />
            <el-table-column label="操作" width="350">
                <template #default="{ row }">

                    <el-button size="small" text type="primary" @click="handleEdit(row)">修改</el-button>

                    <el-popconfirm title="是否要删除?" confirm-button-text="确定" cancel-button-text="取消"
                        @confirm="handleDelete(row.id)">
                        <template #reference>
                            <el-button class="!m-0" text type="primary" size="small">删除
                            </el-button>
                        </template>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>

        <FormDrawer ref="formDrawerRef" @submit="handleSubmit" :title="drawerTitle">
            <el-form :model="form" ref="formRef" :rules="rules" label-width="auto">
                <el-form-item label="名称" prop="name">
                    <el-input v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="積分">
                    <el-input v-model="form.integral"></el-input>
                </el-form-item>
            </el-form>
        </FormDrawer>


    </el-card>
</template>

<script setup>
import { ref } from "vue";
import ListHeader from "~/components/ListHeader.vue";
import FormDrawer from "~/components/FormDrawer.vue";
import { useInitTable, useInitForm } from "~/composables/useCommon.js";
import { getIntegral, createIntegral, updateIntegral, deleteIntegral } from "~/api/integral.js";


const {
    tableData,
    getData,
    loading,
    curpage,
    total,
    limit,
    handleDelete,
    handleStatusChange,
} = useInitTable({
    getList: getIntegral,
    delete: deleteIntegral,
    onGetListSuccess: (res) => {
        tableData.value = res
    }
});


const {
    formDrawerRef,
    formRef,
    form,
    rules,
    drawerTitle,
    handleSubmit,
    handleCreate,
    handleEdit,
} = useInitForm({
    form: {
        name: "",
        integral: ""
    },
    getData,
    rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
    },
    create: createIntegral,
    update: updateIntegral,
});





</script>

<style>
</style>
